package xyz.hubery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;
import xyz.hubery.domain.TurnoverOrder;
import xyz.hubery.service.TurnoverOrderService;
import xyz.hubery.mapper.TurnoverOrderMapper;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author huber
 * @description 针对表【turnover_order(成交订单)】的数据库操作Service实现
 * @createDate 2025-10-16 07:45:08
 */
@Service
public class TurnoverOrderServiceImpl extends ServiceImpl<TurnoverOrderMapper, TurnoverOrder> implements TurnoverOrderService {

    /**
     * 根据委托单id查询成交订单数据
     * @param entrustOrder 委托单id
     * @param type 委托类型 1-买入；2-卖出
     * @return
     */
    @Override
    public List<TurnoverOrder> findByEntrustOrderId(Long entrustOrder, Integer type) {
        List<TurnoverOrder> turnoverOrders = Collections.emptyList();
        if(type == 1) { // 买入
            turnoverOrders = list(new LambdaQueryWrapper<TurnoverOrder>().eq(TurnoverOrder::getBuyOrderId,entrustOrder));
        }else { // 卖出
            turnoverOrders = list(new LambdaQueryWrapper<TurnoverOrder>().eq(TurnoverOrder::getSellOrderId,entrustOrder));
        }
        return turnoverOrders ;
    }

    @Override
    public Page<TurnoverOrder> findByPage(Page<TurnoverOrder> page, Long userId, String symbol, Integer type) {

        if (type == null || type == 0) { //查询所有
            return page(page, new LambdaQueryWrapper<TurnoverOrder>()
                    .and(wrapper -> wrapper
                            .eq(TurnoverOrder::getBuyUserId, userId)
                            .or()
                            .eq(TurnoverOrder::getSellUserId, userId)
                    )
                    .eq(!StringUtils.isEmpty(symbol) && !"0".equals(symbol), TurnoverOrder::getSymbol, symbol)
                    .orderByDesc(TurnoverOrder::getCreated)
            );
        } else if (type == 1) { // 买入的
            return page(page, new LambdaQueryWrapper<TurnoverOrder>()
                    .eq(TurnoverOrder::getBuyUserId, userId)
                    .eq(!StringUtils.isEmpty(symbol) && !"0".equals(symbol), TurnoverOrder::getSymbol, symbol)
                    .eq(TurnoverOrder::getTradeType, type)
                    .orderByDesc(TurnoverOrder::getCreated)
            );
        } else if (type == 2) { // 卖出的
            return page(page, new LambdaQueryWrapper<TurnoverOrder>()
                    .eq(TurnoverOrder::getSellUserId, userId)
                    .eq(!StringUtils.isEmpty(symbol) && !"0".equals(symbol), TurnoverOrder::getSymbol, symbol)
                    .eq(TurnoverOrder::getTradeType, type)
                    .orderByDesc(TurnoverOrder::getCreated)
            );
        }
        return null;
    }

    /**
     * 根据交易对标识符查询市场所有的成交记录
     * @param symbol 交易对标识符
     * @return
     */
    @Override
    public List<TurnoverOrder> getAllMarketTurnoverOrder(String symbol) {
       return list(new LambdaQueryWrapper<TurnoverOrder>()
               .eq(TurnoverOrder::getSymbol,symbol)
               .eq(TurnoverOrder::getStatus,1)
               .orderByDesc(TurnoverOrder::getCreated)
               .last("limit 60")
       );
    }
}




